// pages/signSuccess/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: '',
    userInfo: {},
    userOpenId: '',
    actionID: '',
    count: 0,
    logged: true,
    name: '',
    createrName: '',
    createrOpenId: ''
  },
  onGetUserInfo: async function(e) {
    if (e.detail.userInfo) {
      await this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      })
      await this.searchFromTable(e.detail.userInfo)
      wx.showToast({
        title: '签到成功'
      })
    }
  },
  onClose() {
    wx.showLoading({
      title: '签到失败'
    })
  },
  /** 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (!wx.cloud) {
      wx.showToast({
        title: '服务器错误',
      })
    }
    this.onGetOpenid()
    if(options.actionID) {
      this.getSettingFun()
      //查询活动信息
      this.set(options.actionID)
    }
    else {
      wx.showToast({
        title: '服务器异常，请联系管理员',
        icon: 'loading',
        duration: 99999
      })
      return 
    }
  },
  // 检测是否可以获取用户信息
  getSettingFun() {
    let that = this
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          console.log(' get scope.userInfo')
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              that.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
              that.searchFromTable(res.userInfo)
            }
          })
          return true
        } else {
          that.setData({
            logged: false
          })
          return false
        }
      }
    })
  },
  //根据活动ID返回查询到 活动信息
  set(actionID) {
    console.log(actionID)
    const that = this
    wx.cloud.callFunction({
      name: 'getActionListById',
      data: {
        actionID: actionID
      },
      success: res => {
        console.log(res)
        that.setData({
          actionID : res.result.data[0]._id,
          name : res.result.data[0].name,
          createrOpenId : res.result.data[0].createrOpenId,
          createrName : res.result.data[0].createrName,
        })
      }
    })
  },
  // 表中查询是否签到
  searchFromTable(getData) {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 99999,
      isShow: false
    })
    console.log({
      actionID: this.data.actionID,
      userOpenId: this.data.userOpenId,
      name: getData.nickName,
      avatarUrl: getData.avatarUrl,
      actionName: this.data.name,
      createrOpenId: this.data.createrOpenId,
      createrName: this.data.createrName
    })
    // 云函数登录
    wx.cloud.callFunction({
      name: 'actionSign',
      data: {
        actionID: this.data.actionID,
        userOpenId: this.data.userOpenId,
        name: getData.nickName,
        avatarUrl: getData.avatarUrl,
        actionName: this.data.name,
        createrOpenId: this.data.createrOpenId,
        createrName: this.data.createrName
      },
      success: res => {
        console.log(res)
        
        wx.hideToast({
          success: () => {
          
            if(res.result.count) {
              this.setData({
                isShow: true,
                count: res.result.count
              })
            }else {
              wx.showToast({
                title: '签到失败',
                icon: 'none',
                duration: 99999
              })
            }
          }
        })
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  onGetOpenid: function() {
    // 调用云函数
    let that_ = this
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', res.result.openid)
        that_.setData({
          userOpenId: res.result.openid
        })
      },
      fail: () => {
        wx.showToast({
          title: '获取信息失败，请重新尝试',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
})